package com.controller;

import com.bean.Article;
import com.bean.Comment;
import com.github.pagehelper.PageHelper;
import com.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;

/**
 * Created by Yuicon on 2017/3/20.
 */
@RestController
@CrossOrigin(origins = {"*"}, methods = {RequestMethod.OPTIONS, RequestMethod.POST, RequestMethod.GET})
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @RequestMapping(value = "all", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public List<Article> findAll(int page) {
        return articleService.findAll(page);
    }

    @RequestMapping(value = "secure/all", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public List<Article> findByUserId(@RequestBody int page, ServletRequest req) {
        return articleService.findByUserId(page, req);
    }

    @RequestMapping(value = "{articleId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public Article findOne(@PathVariable String articleId) {
        return articleService.findOne(articleId);
    }

    /**
     * 文章的添加action
     *
     * @param article  需要添加的文章实例
     * @return 添加成功数量
     */
    @RequestMapping(value = "secure/insert", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public int insert(@RequestBody Article article, ServletRequest req) {
        return articleService.insert(article, req);
    }

    /**
     * 文章的删除action
     * @param articleId
     * @param req
     * @return
     */
    @RequestMapping(value = "secure/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public int delete(@RequestBody String articleId, ServletRequest req) {
        return articleService.deleteById(articleId, req);
    }


    @RequestMapping(value = "secure/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public int addComment(@RequestBody Comment comment, ServletRequest req) {
        return articleService.addComment(comment, req);
    }



}
